window.channelInfo = ['5', '微信公众号', 'wxddf2e96cf63c7f5d', 'ADMIN-0510-003'];
// window.cityInfo = ['52', '无锡'];
window.cityInfo = ['1', '北京'];
// window.payMethod = ['03', '00000003'];
window.payMethod = ['03', '00000006'];
var requester = "xbk-wechat-001";
var secret_key = "D14EC4CD5BD00352FDE83C74DEBF2450";

var commonModule = {
  getParamFromUrl: function (name) {
    var rtn = 'not_exist';
    var url = window.location.href;
    if (url.indexOf('?') > -1) {
      var params = url.split("?")[1].split("&");
      params.forEach(function (value, index, array) {
        if (value.indexOf(name + '=') > -1) {
          rtn = value.split("=")[1];
        }
      });
    }
    return rtn;
  },
  isLogin: function () {
    var flag = false;
    var cur_login = sessionStorage['CURLOGIN'];
    if (cur_login == undefined || cur_login == 'undefined') {
      return flag;
    }
    cur_login = JSON.parse(cur_login);
    if (cur_login.AT != undefined && cur_login.AT != 'undefined') {
      flag = true;
    }
    return flag;
  },
  checkMobile: function (mobile) {
    var rep = /^(13[0-9]|15[012356789]|16[6]|17[01356789]|18[0-9]|19[89])[0-9]{8}$/;
    return rep.test(mobile);
  },
  checkPwd: function (s) {
    if (!/^\d{6}$/.test(s)) return false; // 不是6位数字
    if (/^(\d)\1+$/.test(s)) return false; // 全一样

    var str = s.replace(/\d/g, function ($0, pos) {
      return parseInt($0) - pos;
    });
    if (/^(\d)\1+$/.test(str)) return false; // 顺增

    str = s.replace(/\d/g, function ($0, pos) {
      return parseInt($0) + pos;
    });
    if (/^(\d)\1+$/.test(str)) return false; // 顺减
    return true;
  },
  updateCURLOGIN: function (AT, RT, UID, callback) {
    var CURLOGIN = sessionStorage['CURLOGIN'];
    if (CURLOGIN != undefined && CURLOGIN != 'undefined') {
      //更新
      CURLOGIN = JSON.parse(CURLOGIN);
      if (AT != null && AT != 'null' && AT != '') {
        CURLOGIN.AT = AT;
      }

      if (RT != null && RT != 'null' && RT != '') {
        CURLOGIN.RT = RT;
      }

      if (UID != null && UID != 'null' && UID != '') {
        CURLOGIN.UID = UID;
      }

      sessionStorage.setItem('CURLOGIN', JSON.stringify(CURLOGIN));
    } else {
      //新增
      CURLOGIN = {};
      if (AT != null && AT != 'null' && AT != '') {
        CURLOGIN.AT = AT;
      }

      if (RT != null && RT != 'null' && RT != '') {
        CURLOGIN.RT = RT;
      }

      if (UID != null && UID != 'null' && UID != '') {
        CURLOGIN.UID = UID;
      }

      sessionStorage.setItem('CURLOGIN', JSON.stringify(CURLOGIN));
    }
    if (typeof callback == 'function') {
      callback();
    }
  },
  createHead: function (s) {
    var req_headers = headers;
    if (headers == undefined) {
      req_headers = {};
    }
    req_headers.requester = requester;
    if (isSign) {
      req_headers.sign = commonModule.createSign(params);
    }
    if (isAuth) {
      var cur_login = sessionStorage['CURLOGIN'];
      if (cur_login != undefined && cur_login != 'undefined') {
        req_headers.Authorization = JSON.parse(cur_login).AT;
      } else {
        //去登陆
        this.$router.push('/login?redirect_uri=' + encodeURIComponent(location.href));
      }
    }
    return req_headers;
  },
  createSign: function (params) {
    if (params == undefined) {
      params = {};
    } else {
      params.forEach(function (_key) {
        var value = params[_key];
        if (value == null || value == '') {
          delete params[_key]
        }
      }, this);
    }
    params.nonce = commonModule.createNonce(32);
    return md5(commonModule.createSignParse(params) + "key=" + secret_key);
  },
  createNonce: function (len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var maxPos = $chars.length;
    var nonce = '';
    for (var i = 0; i < len; i++) {
      nonce += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return nonce;
  },
  createSignParse: function (params) {
    var keys = Object.keys(params).sort();
    var param_str = '';
    for (var i = 0; i < keys.length; i++) {
      param_str += keys[i] + "=" + params[keys[i]] + "&";
    }
    return param_str;
  }
}

export default commonModule;
